package integration;

import static mathUtils.VecMath.*;

public class Heun extends IntegratorBase {
	
	float[][]sb;
	float[][][]ds;
	
	public Heun(Integrable s) {
		super(s);
		sb = mathUtils.VecMath.vecCloneSpace(sys.state());
		ds = new float[2][][];
		ds[0] = vecCloneSpace(sys.dstate());
		ds[1] = vecCloneSpace(sys.dstate());
	}

	public void finalizeSlope(float dt) {
		vecCopy(sys.state(), sb);
		vecCopy(sys.dstate(), ds[0]);
		takeStep(dt);
		sys.findSlope();
		vecCopy(sys.dstate(), ds[1]);
		vecCopy(sb, sys.state());
		vecZero(sys.dstate());
		vecAddScaled(sys.dstate(), ds[0], 0.5f, sys.dstate());
		vecAddScaled(sys.dstate(), ds[1], 0.5f, sys.dstate());		
	}
	
	public void adjustSpace() {
		sb = mathUtils.VecMath.vecCloneSpace(sys.state());
		ds = new float[2][][];
		ds[0] = vecCloneSpace(sys.dstate());
		ds[1] = vecCloneSpace(sys.dstate());
		
	}
}
